﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MINERP.BLL.UserLogin;
using MINERP.COM;

namespace MINERP.UserLogin
{
    public partial class FrmChangePwd : Form
    {
        UserLoginBLL _bll = new UserLoginBLL();
        private static FrmChangePwd instance = null;
        
        private FrmChangePwd()
        {
            InitializeComponent();
        }

        public static FrmChangePwd GetInstance
        {
            get
            {
                if (instance == null)
                {
                    instance = new FrmChangePwd();
                }
                return instance;
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (CheckData())
            {
                try
                {
                    int result = ChangePwd(txtUserName.Text.Trim(),MD5HashCode.GetHash( txtOldPwd.Text.Trim()), MD5HashCode.GetHash(txtNewPwd.Text.Trim()));
                    if (result < 0)
                    {
                        MessageBox.Show("旧用户密码不正确！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("密码修改成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        ClearText();
                    }
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message, "密码修改失败！", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }

        }

        private int ChangePwd(string userName, string oldPwd, string newPwd)
        {
            return  _bll.ChangePwd(userName, oldPwd, newPwd); 
        }

        private bool CheckData()
        {
            bool flag = true;
            if (txtUserName.Text.Trim() == string.Empty)
            {
                MessageBox.Show("请输入用户名！","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                txtUserName.Focus();
                return false;
            }
            if ( txtOldPwd.Text.Trim()== string.Empty)
            {
                MessageBox.Show("请输入旧密码！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtOldPwd.Focus();
                return false;
            }
            if (txtNewPwd.Text.Trim() == string.Empty)
            {
                MessageBox.Show("请输入新密码！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtNewPwd.Focus();
                return false;
            }
            return flag;
        }

        private void ClearText()
        {
            txtNewPwd.Text = string.Empty;
            txtOldPwd.Text = string.Empty;
            txtUserName.Text = string.Empty;
        }

        private void FrmChangePwd_FormClosing(object sender, FormClosingEventArgs e)
        {
            instance = null;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
